package com.isoftstone.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.isoftstone.pojo.Institution;
import com.isoftstone.service.InstitutionService;

@Controller
public class InstitutionController {
	@Autowired
	private InstitutionService institutionService;

	@RequestMapping("/addinstitution")
	public String addeducate() {
		return "addinstitution";
	}
	@RequestMapping("/toModifyinstitution")
	public String toRegister() {
		return "user/addinstitution";
	}

	@RequestMapping("/detailinstitution")
	public String detailinstitution(HttpServletRequest req,HttpServletResponse resp) {	
		String idsrc = req.getParameter("id");
		int id = Integer.parseInt(idsrc);
		System.out.println("detilid="+id);
		Institution institution = institutionService.detailPlaninstitution(id);
		req.setAttribute("institution", institution);
		return "detailinstitution";

	}
	
	@RequestMapping("/institution")
	public String list(HttpServletRequest req,HttpServletResponse resp) {
		List<Institution> institutions = null;
			  institutions = institutionService.getSummary();
		req.setAttribute("list", institutions);
		return "listinstitution";
	}

	//奖惩删除
	@RequestMapping("/modifyinstitution")
	public String modifyinstitution(Institution institution,HttpServletRequest req,HttpServletResponse resp) {
		String action = req.getParameter("action");
		int re = 0;
		if("deleteinstitution".equals(action)) {
			String idsrc =  req.getParameter("id");
			int id = Integer.parseInt(idsrc);
			System.out.println("deleteid="+id);
			re = institutionService.modifyinstitution(id);
		}else if("updateinstitution".equals(action)){
			re = institutionService.updateinstitution(institution);
		}else{
			re = institutionService.register(institution);
		}
		if(re==1) {
			return list(req,resp);
		}else {
			return "error";
		}
	}
	

	//跳转修改
	@RequestMapping("/updateinstitution")
	public String selectinstitution(HttpServletRequest req,HttpServletResponse resp) {
		String idsrc = req.getParameter("id");
		int id = Integer.parseInt(idsrc);
		Institution institution = institutionService.selectinstitution(id);
		req.setAttribute("institution", institution);
		return "updateinstitution";
	}
}

